<script setup lang="ts">
import type { IndustryItem, IndustryRes } from "./apis/type.ts"
import { ArrowLeft } from "@element-plus/icons-vue"
import { ElMessage } from "element-plus"
import { onMounted, reactive, ref, watch } from "vue"
/* -------------- 数据 -------------- */

import { useRouter } from "vue-router"

import { createEnterpriseAPI, getIndustryListAPI } from "./apis/index.ts" // ① 引入

const props = defineProps<{
  editId?: number
}>()

watch(() => props.editId, (newId) => {
  if (newId) {
    console.log("Received ID:", newId)
    // 加载对应数据
  }
}, { immediate: true })
const ruleFormRef = ref()
const form = reactive({
  name: "", // 企业名称
  legalPerson: "", // 法人
  registeredAddress: "", // 注册地址
  industryCode: 1, // 所在行业
  contact: "", // 企业联系人
  contactNumber: "", // 联系人电话
  businessLicenseUrl: "baidu.com", // 营业执照 url
  businessLicenseId: 0 // 营业执照 id
})
// const industryList: IndustryItem[] = ref([]) // 所在行业列表
const industryList = ref<IndustryItem[]>([])
/* -------------- 方法 -------------- */
const router = useRouter()
const goBack = () => router.back()

/** 自定义上传 */
async function getIndustryList() {
  const res = await getIndustryListAPI() as IndustryRes
  industryList.value = res.data
}
onMounted(() => {
  getIndustryList()
})

function reset() {
  Object.assign(form, {
    name: "",
    legalPerson: "",
    registeredAddress: "",
    industryCode: "",
    contact: "",
    contactNumber: "",
    businessLicenseUrl: "",
    businessLicenseId: ""
  })
}

async function submit() {
  await createEnterpriseAPI(form) // ③ 直接传 form
  ElMessage.success("提交成功")
  console.log("提交内容", form)
}
</script>

<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="goBack">
          <el-icon><ArrowLeft /></el-icon>返回
        </span>
        <span>|</span>
        <span>添加企业</span>
      </div>
    </header>

    <main class="add-main">
      <div class="form-container">
        <div class="title">
          企业信息
        </div>
        <div class="form">
          <el-form ref="ruleFormRef" label-width="100px">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>

            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="form.legalPerson" />
            </el-form-item>

            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="form.registeredAddress" />
            </el-form-item>

            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="form.industryCode">
                <el-option
                  v-for="item in industryList"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="form.contact" />
            </el-form-item>

            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="form.contactNumber" />
            </el-form-item>

            <el-form-item label="营业执照">
              <el-upload
                action="#"
              >
                <el-button size="small" type="primary">
                  点击上传
                </el-button>
                <template #tip>
                  <div class="el-upload__tip">
                    只能上传jpg/png文件，且不超过500kb
                  </div>
                </template>
              </el-upload>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>

    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="reset">
          重置
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
.add-enterprise {
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;

    .left {
      display: flex;
      align-items: center;
      gap: 8px;
      .arrow {
        cursor: pointer;
        display: inline-flex;
        align-items: center;
      }
    }
  }

  .add-main {
    padding: 20px 130px;

    .form-container {
      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
      .form {
        padding: 20px 65px 24px;
        .el-form {
          display: flex;
          flex-wrap: wrap;
          .el-form-item {
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    text-align: center;
  }
}
</style>
